<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Missions' );
?>
					<div class="blok">
<?php 
print_operations_tab( 'Missions' );
?>
				</div>
				<br />
<?php

if( isset( $_SESSION[ 'start_mission_status' ] ) ) {
	print $_SESSION[ 'start_mission_status' ] . "<br /><br />\r\n";
	unset( $_SESSION[ 'start_mission_status' ] );
}
if( isset( $_SESSION[ 'start_mission2_status' ] ) ) {
	print $_SESSION[ 'start_mission2_status' ] . "<br /><br />\r\n";
	unset( $_SESSION[ 'start_mission2_status' ] );
}

	$query = "SELECT * FROM army WHERE ruler_id = " . $_SESSION['ruler_id'] . " ORDER BY name";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$num = mysql_numrows( $result );
	
	if( $num > 0 ) {
		print "<form action=\"start_mission.php\" method=\"post\">";
	    print "<table><th colspan=\"4\">" . translate( 'Start a mission' ) . ":</th>\r\n";
	    print "<tr><td>" . translate( 'Select' ) . "</td<td>" . translate( 'Name' ) . "</td><td>" . translate( 'Officer' ) . "</td><td>" . translate( 'Status' ) . "</td></tr>\r\n";
	    $i = 0;
	    while ( $i < $num ) {
	        if( $i % 2 == 0 ) {
	            print "<tr id=\"even\">";
	        }
	        else {
	            print "<tr>";
	        }
	
	    	if( mysql_result( $result, $i, "training" ) > 0 ) {
				$action = translate( 'Training' );
				$ready = false;
			}
			elseif( mysql_result( $result, $i, "travel" ) > 0 ) {
				$action = translate( 'Moving' );
				$ready = false;
			}
			elseif( mysql_result( $result, $i, "follow" ) > 0 ) {
				$action = translate( 'Following an army' );
				$ready = false;
			}
			elseif( mysql_result( $result, $i, "mission" ) > 0 ) {
				$action = translate( 'On a mission' ) . ", " . mysql_result( $result, $i, "mission" ) . " " . translate( 'days remaining' ) . ".";
				$ready = false;
			}
			else {
				$action = translate( 'Waiting for orders' );
				$ready = true;
			}
	
	        $query2 = "SELECT name FROM officers WHERE officer_id = " . mysql_result( $result, $i, "officer_id" );
	        $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	        $row = mysql_fetch_array( $result2, MYSQL_ASSOC );
	        if( isset( $row[ 'name' ] ) ) {
	        	$officer_name = "<a href=\"../armies/officer.php?name=" .  $row[ 'name' ] . "\">" . $row[ 'name' ] . "</a>";
	        }
	        else {
	        	$officer_name = translate( 'None' );
	        }
	
	        if( $ready ) {
	        	print "<td><input type=\"checkbox\" name=\"" . mysql_result( $result, $i, "army_id" ) . "\" ></td>";
	        }
	        else {
	        	print "<td></td>";
	        }
	
	        print "<td> <a href=\"../armies/army.php?name=" . mysql_result( $result, $i, "name" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a></td><td>$officer_name</td><td>" . $action;
	        print "</td></tr>\r\n";
	        $i ++;
	    }
		print "<tr><td></td><td><input type=\"submit\" value=\"" . translate( 'Select for a mission' ) . "\" /></td><td></td><td></td></tr>";
		print "</form></table>\r\n\r\n";
	}
	else {
		print( "<div class=\"blok\">\r\n" );
		print( translate( 'You have no armies to start a mission with.' ) . "<br />\r\n" );
		print( "</div>\r\n" );
	}

aos_footer();
?>